<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :error.sync="error"
      error-text="加载失败，请点击重试"
      @load="onLoad"
    >
      <van-cell
        v-for="(article, index) in list"
        :key="index"
        :title="article.title"
        @click="goDetailPath(article.textid)"
      />
    </van-list>
  </div>
</template>

<script>
import { list } from '@/static/article-list'
export default {
  name: 'SearchResult',
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false
    }
  },
  methods: {
    onLoad () {
      this.loading = true
      this.finished = false
      this.list = []
      setTimeout(() => {
        list.forEach(val => {
          var arr = val.result.filter(val => val.title.search(this.searchText) !== -1)
          this.list.push(...arr)
        })
        this.loading = false
        this.finished = true
      }, 1000)
    },
    goDetailPath (id) {
      this.$router.push(`/text/${id}`)
    },
    isShow () {
      return this.list.length === 0
    }
  }
}
</script>

<style scoped lang="less">
</style>
